package com.kdtech.analyse.Bbs.common;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 * Discuz!NT 3.5.2  版本
<meta name="generator" content="Discuz!NT 3.5.2" />
<meta name="author" content="Discuz! Team and Comsenz UI Team" />

 * 0000
 * http://www.xxx.xxx/ XX论坛
 * @author KK
 */
public class BbsAnalyseDiscuzV3 implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		url = UrlArgumentTop.FromatUrl(url);
		url = StringUtils.substringAfterLast(url, "/");
		String[] regex = {
				"showtopic-[0-9]+.aspx",
				"showtopic-[0-9]+.htm",
				"showtopic-[0-9]+-1.aspx",
				"thread-[0-9]*.html",
				"showtopic-[0-9]+.html",
				"showtopic.aspx\\?page=end&topicid=[0-9]*",
				"forum.php\\?extra=.*&mod=viewthread&tid=[0-9]*"
				};
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
			return null;
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String updateUrl = url;
		Document doc = Jsoup.parse(htmltxt);
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		String author = null;
		Elements select = doc.select("div.msg_inner.error_msg");
		if(select.size()>0){
			return null;
		}
		title = doc.select("title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBefore(title, "-");
		}
		if (StringUtils.isBlank(title)){
			title = doc.select("div.spaceborder").text();
		}
		
		Element firstDiv = doc.select("div#postsContainer > table[id]").first();
		if (firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select("div.postinfo span[title]").attr("title"));
			if(date==null){
				date = DateUtils.matchDate(firstDiv.select("div.postinfo").text());
			}
			content = HtmlCleaner.getContentHtml(url,firstDiv.select("div#firstpost"));
			author = firstDiv.select("td.postauthor div.poster span.onlineyes").text();
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("div.poster span.onlineno").text();
			}
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("a.dropmenu").text();
			}
		}
		
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("table.t_msg").text());
		}
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("div.postinfoleft").text());
		}
		if(StringUtils.isBlank(content))
		{
			content = doc.select("div.t_msgfont").text();
		}
		if(StringUtils.isBlank(author))
		{
			Elements authorList = doc.select("td.t_user strong");
			if(authorList.size()>0)
			{
				author = authorList.get(0).text();
			}
		}
		if(StringUtils.isBlank(author))
		{
			Elements authorList = doc.select("div.normalname");
			if(authorList.size()>0)
			{
				author = authorList.get(0).text();
			}
		}
		

		String commTxt = doc.select("div#postsContainer td.postauthor div.hm").text();
		if(StringUtils.isNotBlank(commTxt)){
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "|"));
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "|"));
		}
		if(StringUtils.isBlank(commTxt)){
			 commTxt = doc.select("p.forumstats").text();
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "/"));
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "/"));
		}

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(4);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setClickNum(clickNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	

}
